var reqData = {};

reqData.getClientIp = function (req) {
	return (req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress);
}

// 从req.headers.cookie中获取cookie值
reqData.getResCookie = function (req, name) {
  var cookie = req
  var cookieObj = {}
  var cookieArr = []
  var key = ''
  var value = ''
  cookie = cookie.split(';')
  for (let i = 0; i < cookie.length; i++) {
    cookieArr = cookie[i].trim().split('=')
    key = cookieArr[0]
    value = cookieArr[1]
    cookieObj[key] = value
  }
  if (cookieObj[name]) {
    return cookieObj[name]
  } else {
    return ''
  }
}

module.exports = reqData;
